import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

public class CalendarGraphic
{
	public CalendarGraphic()
    {
        month = new Month();
        init();
    }
    public CalendarGraphic(Date date)
    {
        month = new Month(date);
        init();
    }
	public void setSingleSelection(boolean type)
	{
		month.setSingleSelection(type);
	}
	public void setNormalSelectionMode(boolean mode)
	{
		month.setNormalSelectionMode(mode);
		month.update();
	}
    private void init()
    {
    	JPanel topPanel = new JPanel(new GridLayout(1,2));
        topPanel.add(getSelectMonthPanel());
        topPanel.add(getSelectYearPanel());
        calendarPanel = new JPanel(new BorderLayout());
        calendarPanel.add(topPanel,BorderLayout.NORTH);
        calendarPanel.add(month.getPanel(),BorderLayout.CENTER);
    }
    private JPanel getSelectYearPanel()
    {
        JPanel yearPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JSpinner yearLabel = new JSpinner(new SpinnerYearModel(month.getYear()));
        yearLabel.setFont(Resources.getFont(Resources.FONT_TYPE.CALENDAR_LABEL));
        yearLabel.addChangeListener(new
    		ChangeListener()
        	{
				public void stateChanged(ChangeEvent e) 
				{
					JSpinner aux = (JSpinner) e.getSource();
					try
					{
						month.setYear(new Integer(aux.getValue().toString()));
					}catch(NumberFormatException ne)
					{
						
					}
				}
        	}
		);
        yearPanel.add(yearLabel);
        return yearPanel;
    }
    private JPanel getSelectMonthPanel()
    {
        JPanel monthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JSpinner monthLabel = new JSpinner(new SpinnerMonthModel(month.getMonth()));
        monthLabel.setFont(Resources.getFont(Resources.FONT_TYPE.CALENDAR_LABEL));
        monthLabel.setPreferredSize(new Dimension(110,25));
        monthLabel.setValue(Resources.getString(month.getNameMonth()));
        monthLabel.addChangeListener(new
        		ChangeListener()
            	{
    				public void stateChanged(ChangeEvent e) 
    				{
    					JSpinner aux = (JSpinner) e.getSource();
    					SpinnerMonthModel auxModel = (SpinnerMonthModel)aux.getModel();
    					month.setMonth(auxModel.getSelectedIndex());
    				}
            	}
    		);
        monthPanel.add(monthLabel);
        return monthPanel;
    }
    public JPanel getPanel()
    {
        return calendarPanel;
    }
    public Date[] getSelectedDays()
    {
    	return month.getSelectedDays();
    }
    public void setDaysMarked(Date[] days_)
    {
    	month.setDaysMarked(days_);
    	month.update();
    }
    public void setSelectedDays(Date[] dates)
    {
    	month.setSelectedDays(dates);
    }
    private class SpinnerYearModel extends AbstractSpinnerModel
    {
    	public SpinnerYearModel(int initYear)
    	{
    		year = new Integer(initYear);
    	}
    	public Object getValue()
    	{
    		return year.toString();
    	}
    	public void setValue(Object value)
    	{
    		int aux = year;
    		try
    		{
    			year = new Integer(value.toString());
    		}catch(NumberFormatException e)
    		{
    			year = aux;
    			throw new IllegalArgumentException();
    		}
    		fireStateChanged();
    	}
    	public Object getNextValue()
    	{
    		return year + 1;
    	}
    	public Object getPreviousValue()
    	{
    		return year - 1;
    	}
    	Integer year;
    }
    private class SpinnerMonthModel extends AbstractSpinnerModel
    {
    	public SpinnerMonthModel(int initMonth)
    	{
    		monthIndex = initMonth;
    		months = new String[12];
    		for(int i = 0; i < 12; i++)
            	months[i] = Resources.getString(month.getNameMonth(i));
    	}
    	public int getSelectedIndex()
    	{
    		return monthIndex;
    	}
    	public Object getValue()
    	{
    		return months[monthIndex];
    	}
    	public void setValue(Object value)
    	{
    		if (!(value instanceof String))
    			throw new IllegalArgumentException();
    		for(int i = 0; i < 12; i++)
    			if(months[i].equals(value.toString()))
    			{
    				monthIndex = i;
    				break;
    			}
    		fireStateChanged();
    	}
    	public Object getNextValue()
    	{
    		int aux = monthIndex+1;
    		if(aux > 11)
    			aux = 11;
    		return months[aux];
    	}
    	public Object getPreviousValue()
    	{
    		int aux = monthIndex-1;
    		if(aux < 0)
    			aux = 0;
    		return months[aux];
    	}
    	private int monthIndex;
    	private String[] months;
    }
    private Month month;
    private JPanel calendarPanel;
}
